/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package redclient.Items;

/**
 *
 * @author Isaias
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TimeEntry implements Serializable {
    
    private String issueId;
    
    private String projectId;
    
    private Date spentOn;
    
    private float hours;
    
    private String activityId;
    
    private String comments;

    private List<CustomField> customFields;
    
    public String getTimeEntry()
    {
        return null;
    }

    /**
     * @return the issueId
     */
    public String getIssueId() {
        return issueId;
    }

    /**
     * @param issueId the issueId to set
     */
    public void setIssueId(String issueId) {
        this.issueId = issueId;
    }

    /**
     * @return the projectId
     */
    public String getProjectId() {
        return projectId;
    }

    /**
     * @param projectId the projectId to set
     */
    public void setProjectId(String projectId) {
        this.projectId = projectId;
    }

    /**
     * @return the spentOn
     */
    public Date getSpentOn() {
        return spentOn;
    }

    /**
     * @param spentOn the spentOn to set
     */
    public void setSpentOn(Date spentOn) {
        this.spentOn = spentOn;
    }

    /**
     * @return the hours
     */
    public float getHours() {
        return hours;
    }

    /**
     * @param hours the hours to set
     */
    public void setHours(float hours) {
        this.hours = hours;
    }

    /**
     * @return the activityId
     */
    public String getActivityId() {
        return activityId;
    }

    /**
     * @param activityId the activityId to set
     */
    public void setActivityId(String activityId) {
        this.activityId = activityId;
    }

    /**
     * @return the comments
     */
    public String getComments() {
        return comments;
    }

    /**
     * @param comments the comments to set
     */
    public void setComments(String comments) {
        this.comments = comments;
    }

    /**
     * @return the customFields
     */
    public List<CustomField> getCustomFields() {
        return customFields;
    }

    /**
     * @param customFields the customFields to set
     */
    public void setCustomFields(List<CustomField> customFields) {
        this.customFields = customFields;
    }

    public static void Serialize(Project project, TimeEntry timeEntry) throws IOException
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        File cache = new File("cache");
        File timeEntryFile = new File(cache, 
                                      String.format("%1$s_%2$s_%3$s_%4$s.dat",
                                                    project.getCredentials().getInternalId(),
                                                    timeEntry.projectId,
                                                    timeEntry.issueId,
                                                    sdf.format(new Date()))
                                    );
        FileOutputStream fos = new FileOutputStream(timeEntryFile);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(timeEntry);
        oos.close();
    }
    
    public static Map<Integer, List<TimeEntry>> getTimeEntries() throws IOException, ClassNotFoundException
    {
        File cache = new File("cache");
        FilenameFilter textFilter = new FilenameFilter() {
            private Pattern pattern = Pattern.compile("(\\d+_){3}\\d{14}\\.(dat)");
            
            public boolean accept(File dir, String name) {
                Matcher m = pattern.matcher(name);
                return m.matches();
            }
        };

        Map<Integer, List<TimeEntry>> timeEntries = new HashMap<Integer, List<TimeEntry>>();
        for( File file : cache.listFiles(textFilter))
        {
            String[] data = file.getName().split("_");
            Integer internalId = Integer.valueOf(data[0]);
            if( !timeEntries.containsKey(internalId) )
            {
                timeEntries.put(internalId, new ArrayList<TimeEntry>());
            }
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            TimeEntry timeEntry = (TimeEntry)ois.readObject();
            timeEntries.get(internalId).add(timeEntry);
        }
        return timeEntries;
    }

    @Override
    public String toString() {
        return comments;
    }
    
    
}
